package com.ilink.ilinkcommon.domain;

import com.ilink.ilinkcommon.exception.CommonException;
import lombok.Data;

/**
 * <h3>ilink-java-backend</h3>
 *
 * @author HuangJiayu
 * @description <p>固定返回类型R</p>
 * @date 2024-09-14 16:05
 **/
@Data
public class R<T> {
	private int code;
	private String msg;
	private T data;
	
	public static R<Void> ok() {
		return ok(null);
	}
	
	public static <T> R<T> ok(T data) {
		return new R<>(200, "OK", data);
	}

	public static <T> R<T> ok(String msg, T data) {
		return new R<>(200, msg, data);
	}

	public static <T> R<T> error(String msg) {
		return new R<>(500, msg, null);
	}
	public static <T> R<T> error(String msg, T data) {
		return new R<>(500, msg, data);
	}
	
	public static <T> R<T> error(int code, String msg) {
		return new R<>(code, msg, null);
	}
	
	public static <T> R<T> error(CommonException e) {
		return new R<>(e.getCode(), e.getMessage(), null);
	}
	
	public R() {
	}
	
	public R(int code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}
	
	public boolean success(){
		return code == 200;
	}
}
